package gold.digger;

import java.util.*;
import java.util.Arrays;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC826 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
            Map<Integer, Integer> map = new HashMap<>();
            Map<Integer, Integer> map_max = new LinkedHashMap<>();
            for (int i = 0; i < difficulty.length; i++) {
                if (null != map.get(difficulty[i])) {
                    if (map.get(difficulty[i]) < profit[i]) {
                        map.put(difficulty[i], profit[i]);
                    }
                } else {
                    map.put(difficulty[i], profit[i]);
                }
            }
            Arrays.sort(difficulty);
            int max = 0;
            for (int i = 0; i < difficulty.length; i++) {
                if (max < map.get(difficulty[i])) {
                    max = map.get(difficulty[i]);
                }
                map_max.put(difficulty[i], max);
            }
            int res = 0;
            for (int w : worker) {
                for (int i = 0; i < difficulty.length - 1; i++) {
                    if (w == difficulty[i] || (w > difficulty[i] && w < difficulty[i + 1])) {
                        res += map_max.get(difficulty[i]);
                        break;
                    }
                    if (w >= difficulty[difficulty.length - 1]) {
                        res += map_max.get(difficulty[difficulty.length - 1]);
                        break;
                    }
                }
            }
            return res;
        }
    }


    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC826 an = new LC826();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
